package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.example.domain.po.*;
import org.example.service.ErpSupplierService;
import org.example.service.impl.ErpSupplierServiceImpl;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basic/vendor")
public class ErpSupplierController {

    @Autowired
    private ErpSupplierService erpSupplierService;

    @GetMapping("/list")
    public R<List<ErpSupplier>> list() {
        return R.success(erpSupplierService.selectByExample());
    }
    @GetMapping("/list2")
    public R<List<ErpSupplier>> list2() {
        return R.success(erpSupplierService.selectByExample2());
    }
    @GetMapping("/list3")
    public R<List<ErpSupplier>> list3() {
        return R.success(erpSupplierService.selectByExample3());
    }

    //分页条件查询
    @GetMapping("/find")
    public Result<Page<ErpSupplier>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size,
            @RequestAttribute("user") ErpUser user,
            String supName,
            String supPhonenumber,
            String supContactnumber,
            Integer supType
    ){
        Page<ErpSupplier> page = new Page<>(current, size);
        page = erpSupplierService.findByPage(page, supName, supPhonenumber, supContactnumber, supType, user.getTenantId());
        return Result.success(page);
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestAttribute("user") ErpUser user, @RequestBody ErpSupplier erpSupplier){
        erpSupplier.setTenantId(Math.toIntExact(user.getTenantId()));
        return erpSupplierService.save(erpSupplier) ? Result.success("新增成功！") : Result.error("新增失败！");
    }

    @PutMapping("/update")
    public Result update(@RequestBody ErpSupplier erpSupplier){
        return erpSupplierService.updateById(erpSupplier) ? Result.success("编辑成功！") : Result.error("编辑失败！");
    }

    @DeleteMapping("/{supId}")
    public Result<String> remove(@PathVariable("supId") Integer supId){
        return erpSupplierService.removeById(supId) ? Result.success("删除成功！") : Result.error("删除失败！");
    }

    //批量删除
    @PostMapping("/batch")
    public Result<String> removeAll(@RequestBody List<ErpSupplier> erpSuppliers){
        return erpSupplierService.removeBatchByIds(erpSuppliers) ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    //批量修改
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpSupplierService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.error("操作失败！");
    }

    //查询供应商
    @GetMapping("/findSuppliers")
    public Result<List<ErpSupplier>> findSuppliers(){
        return Result.success(erpSupplierService.findSuppliers());
    }

    /**
     * 查询启用
     * @param user
     * @param supType
     * @return
     */
    @GetMapping("/findByStatus")
    public Result<List<ErpSupplier>> findByEnabled(@RequestAttribute("user") ErpUser user,
                                                   @RequestParam("supType") Integer supType) {
        QueryWrapper<ErpSupplier> wrapper = new QueryWrapper<>();
        //查询状态为启用的
        wrapper.eq("status",1);
        wrapper.eq("delete_flag",0);
        wrapper.eq("tenant_id", user.getTenantId());
        wrapper.eq("sup_type", supType);
        List<ErpSupplier> list = erpSupplierService.list(wrapper);
        return Result.success(list);
    }

    //查询会员
    @GetMapping("/findSupplier3")
    public Result<List<ErpSupplier>> findSupplier3(){
        return Result.success(erpSupplierService.findSupplier3());
    }
    @GetMapping("findBy")
    public R<List<ErpSupplier>>findBy(){
        return R.success(erpSupplierService.findBy());
    }
    
    @GetMapping("/findMemberAndType")
    @ApiOperation("根据类型查询该租户下的supplier表")
    public Result<List<ErpSupplier>> findMemberAndType(@RequestAttribute("user")ErpUser user, @RequestParam(value = "subType") List<Long> subType){
        return Result.success(erpSupplierService.findMemberAndType(user.getTenantId(), subType));
    }
    
}
